<?php

class RequestController extends Controller
{
	public function filters()
	{
		return array(
			'accessControl',
			'ajaxOnly'
		);
	}

	public function accessRules()
	{
		return array(
			array('allow',
				'actions'=>array(
					'viewListEvent',
                    'suggestMember'
				),
				'users'=>array('*'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	public function actionSuggestMember($term)
	{
		$model = Members::model()->findAll('(username LIKE "%' . $term . '%" OR email LIKE "%' . $term . '%") AND status="A"');
		$result = array();
		if (!empty($model)) {
			foreach ($model as $data) {
				$result[] = array('label'=>$data->username . ' - ' . $data->email, 'value'=>$data->username . ' - ' . $data->email, 'id'=>$data->id);
			}
		}
		
		echo CJSON::encode($result);
	}

	public function actionViewListEvent(){
		if(isset($_GET['act'])){
		    $act = $_GET['act']; 
		    $condition = '';
		    $class_view = 'badge-danger';
		    if($act != 'doCreatePost'){
		        if($act=='doMostView'){
		            $condition = '';
		        }
		        if($act=='doNearShow'){
		        	$condition = '';
		        	$class_view = 'badge-default';
		        }
		        if($act=='doMostView2'){
		        	$condition = '';
		        	$class_view = 'badge-success';
		        }
				$model = Events::model()->findAllByAttributes(array(),array(
						        'limit'=>10,
						        //'order'=>'posteddate DESC',
						        'condition'=>'status="A" AND enddate >= NOW()'	));
				$this->renderPartial('/default/_viewEvent',array('model'=>$model,'class_view'=>$class_view));	        	
			}else{
				$criterial = new CDbCriteria();
				$criterial->limit = 10 ;
				$criterial->order = 'count(member_id) DESC';
				$criterial->group = 'member_id';
				$model = Events::model()->findAll($criterial);
				$this->renderPartial('/default/_viewUsers',array('model'=>$model,'class_view'=>$class_view));	        	
			}
	    }else
            throw new Exception("Sorry",500);
	}
}